package com.jiutong.meipenggang.ui.activitys;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.MainPageBean;
import com.jiutong.meipenggang.entity.TotalMoneyBean;
import com.jiutong.meipenggang.http.model.interfaces.MainPageBiz;
import com.jiutong.meipenggang.http.present.MainPagePresent;
import com.jiutong.meipenggang.utils.DensityUtil;
import com.jiutong.meipenggang.utils.NumUilt;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.MyPieChart;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class TotalMoneyActivity extends BaseActivity implements MainPageBiz {

    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.tv_common_title_options)
    TextView tvCommonTitleOptions;
    @Bind(R.id.tv_total_money_income)
    TextView tvTotalMoneyIncome;
    @Bind(R.id.tv_total_money_keyong)
    TextView tvTotalMoneyKeyong;
    @Bind(R.id.tv_total_money_keyong_rate)
    TextView tvTotalMoneyKeyongRate;
    @Bind(R.id.tv_total_money_meicaibao)
    TextView tvTotalMoneyMeicaibao;
    @Bind(R.id.tv_total_money_meicaibao_rate)
    TextView tvTotalMoneyMeicaibaoRate;
    @Bind(R.id.tv_total_money_meilicai)
    TextView tvTotalMoneyMeilicai;
    @Bind(R.id.tv_total_money_meilicai_rate)
    TextView tvTotalMoneyMeilicaiRate;

    private MyPieChart myPieChart;
    private double keyong, meilicai, meicaibao;
    private MainPagePresent present;
    private double balance;
    private double investBalance;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_total_money;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("总资产");
    }

    @Override
    protected void onResume() {
        super.onResume();
        present = new MainPagePresent(this, this);
        present.TotalMoney();
    }

    private void setDatetosql() {
        tvCommonTitleOptions.setVisibility(View.VISIBLE);
        tvCommonTitleOptions.setText("详情");

        myPieChart = (MyPieChart) findViewById(R.id.mypiechart);
        tvTotalMoneyKeyong.setText(NumUilt.formatDouble((keyong)));
        tvTotalMoneyMeilicai.setText(NumUilt.formatDouble(meilicai));
        tvTotalMoneyMeicaibao.setText(NumUilt.formatDouble(meicaibao));

        myPieChart.setRadius(DensityUtil.dp2px(this, 80));

//        myPieChart.setOnItemClickListener(this);

        List<MyPieChart.PieEntry> pieEntries = new ArrayList<>();
        //可用余额百分比
        double keyongRate = keyong == 0 ? 0 : (keyong / (keyong + meilicai + meicaibao) * 100);
        //美财宝百分比
        double meicaibaoRate = meicaibao == 0 ? 0 : (meicaibao / (keyong + meilicai + meicaibao) * 100);
        //美理财百分比
        double meilicaiRate = meilicai == 0 ? 0 : (meilicai / (keyong + meilicai + meicaibao) * 100);
        if (keyong == meilicai && meilicai == meicaibao && keyong != 0) {
            tvTotalMoneyKeyongRate.setText("约" + NumUilt.formatDouble(keyongRate) + "%");
            //第三个参数true or flase是用来判断圆形中的一部分扇形是否突出true 是 false 否
            pieEntries.add(new MyPieChart.PieEntry(Float.parseFloat(NumUilt.formatDouble(keyongRate)), R.color.gesture_color, false, "可用余额"));
            pieEntries.add(new MyPieChart.PieEntry(Float.parseFloat(NumUilt.formatDouble(meicaibaoRate)), R.color.meicaibao_color, false, "美财宝"));
            pieEntries.add(new MyPieChart.PieEntry(Float.parseFloat(NumUilt.formatDouble(meilicaiRate)), R.color.meilicai_color, false, "美理财"));
            myPieChart.setPieEntries(pieEntries);
        } else if(keyong == meilicai && meilicai == meicaibao && keyong == 0) {
            tvTotalMoneyKeyongRate.setText("约" + NumUilt.formatDouble(keyongRate) + "%");
        }else{
            tvTotalMoneyKeyongRate.setText("约" + NumUilt.formatDouble(keyongRate) + "%");
            pieEntries.add(new MyPieChart.PieEntry(Float.parseFloat(NumUilt.formatDouble(keyongRate)), R.color.gesture_color, false, "可用余额"));
            pieEntries.add(new MyPieChart.PieEntry(Float.parseFloat(NumUilt.formatDouble(meicaibaoRate)), R.color.meicaibao_color, false, "美财宝"));
            pieEntries.add(new MyPieChart.PieEntry(Float.parseFloat(NumUilt.formatDouble(meilicaiRate)), R.color.meilicai_color, false, "美理财"));
            myPieChart.setPieEntries(pieEntries);
        }
        tvTotalMoneyMeicaibaoRate.setText("约" + NumUilt.formatDouble(meicaibaoRate) + "%");
        tvTotalMoneyMeilicaiRate.setText("约" + NumUilt.formatDouble(meilicaiRate) + "%");
    }


    @OnClick({R.id.rl_common_title_back, R.id.tv_common_title_options})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.tv_common_title_options:
                openActivity(BillDetailActivity.class);
                break;
            default:
                break;
        }
    }

    @Override
    public String getPersonalId() {
        return null;
    }

    @Override
    public String getToken() {
        return null;
    }

    @Override
    public void onMainPageSuccess(MainPageBean subjects) {

    }

    @Override
    public void onSuccess(Object object) {
        TotalMoneyBean respone = (TotalMoneyBean) object;
        if(respone.getError()==-1){
            SpConfig.getInstance().edit().putString(Constants.ID, respone.getId().toString()).commit();
            SpConfig.getInstance().edit().putString(Constants.TOKEN, respone.getToken()).commit();
            investBalance =  respone.getInvestBalance();
            balance = respone.getBalance();
            tvTotalMoneyIncome.setText(NumUilt.formatString(respone.getYesterdayIncome()+""));
            tvTotalMoneyKeyong.setText(balance + "");
            tvTotalMoneyMeilicai.setText(respone.getInvestBalance() + "");
            tvTotalMoneyMeicaibao.setText(respone.getTreasureBalance() + "");
            keyong = Double.parseDouble(respone.getBalance() + "");
            meilicai = Double.parseDouble(respone.getInvestBalance() + "");
            meicaibao = Double.parseDouble(respone.getTreasureBalance() + "");

            setDatetosql();
        }else {
            toastShortShow(respone.getMsg());
        }

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }
}
